<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use \Illuminate\Http\Request;
use \Illuminate\Support\Facades\DB;
use \Illuminate\Support\Facades\Storage;
use \Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Route;

use App\Models\Users;
use App\Models\Organization;

class UserController extends Controller
{
    //获取用户数据
    private function getUserDataPrivate($data){
        $result = null;
        if(!isset($data['id'])){
            $result = Users::where('account', $data['user'])->where('password', md5($data['password']))->select(['users.nickname','id'])->first();
            
        }else{
            $result = Users::where('id',$data['id'])->select(['users.nickname','id'])->first();
        }

        if($result){
            $result->organization = $this->getOrganizationPrivate($result->id);
        }
        return $result;
    }

    private function getOrganizationPrivate($userId){
        $organization = [];
        $sql = 'SELECT `organization`.`id`,`organization`.`organization_class_id`,`organization`.`name` FROM `organization_users_associated` JOIN `organization` WHERE `organization_users_associated`.`users_id` = '.$userId.' AND `organization_users_associated`.`organization_id` = `organization`.`id`;';
        $organization = DB::select($sql);
        foreach($organization as $key=>$row){
            $sql = 'SELECT `id`,`name`,`created_at` FROM `organization_user_task` WHERE `organization_id` = '.$row->id;
            $organization[$key]->task = DB::select($sql);
        }

        return $organization;
    }

    //获取用户数据
    public function getUserData(Request $request){
        $result = ['code'=>1];
        $result['data'] = $this->getUserDataPrivate(['id'=>$request->user->id ]);
        return $result; 
    }

    
    //获取用户的班级作业
    public function getTaskFolder(Request $request){
        $result = ['code'=>1];
        $filesRoute = 'taskFolder/'.$request->input('organization').'/'.$request->input('organization_class').'/'.$request->input('organization_user_task');
  
        
        $dir = [];
        

        $directoriesAndFiles = Storage::disk('public')->allfiles($filesRoute);
        
        foreach ($directoriesAndFiles as $item) {
            array_push($dir,str_replace($filesRoute,'',$item));
        }

        $result['data'] = ['dir'=>$dir,'path'=>'http://localhost/informationSystem'.Storage::url('app/public/'.$filesRoute)];

        return $result; 
    }

    //获取用户的班级信息
    public function getOrganization(Request $request){
        $result = ['code'=>1];
        $result['data'] = $this->getOrganizationPrivate();
        return $result;
    }


    public function login(Request $request){
        $result = ['code'=>1];
        $user =  $this->getUserDataPrivate(['user'=>$request->input('user'),'password'=>$request->input('password')]);
        if($user){
            $user->token = md5(time()+rand());
            $results = DB::select("UPDATE `users` SET `token` = '".$user->token."' WHERE `users`.`id` = ".$user->id);
            //$user->save();
            $result['data'] = $user;
        }else{
            $result['code'] = 0;
            $result['message'] = '账号或者密码错误';
        }
        return $result;
    }

    
}